﻿using commonImportPlugin.Configs.Base;
using commonImportPlugin.Configs.Other;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace commonImportPlugin.Configs.Other
{
    //
    //  普通模式
    //
    #region 
    /// <summary>
    /// 匹配配置
    /// </summary>
    public class MatchConfig : ConfigsBase,IcommonBase
    {
        //
        //    基本配置参数
        //
        #region
        /// <summary>
        /// 工厂配置
        /// </summary>
        public string factory { get; set; }
        /// <summary>
        /// 本匹配该字段没用
        /// </summary>
        public string fileFilterRules { get; set; }
        /// <summary>
        /// 匹配参数
        /// </summary>
        public string[] matchParameters { set; get; }
        /// <summary>
        /// 错误是否继续
        /// </summary>
        public bool notMatchIsContinue { set; get; }
        #endregion

        //
        //    其他
        //
        #region
        private static MatchConfig uniqueInstance;
        public void Init()
        {
            MatchConfig matchConfig = new MatchConfig();

            //工厂配置
            matchConfig.factory = configs["MatchFactory"];

            //匹配参数
            string matchRegex = @"^([^\^]+)\^([^\^]+)\^([^\^]+)\^([^\^]*)\^([^\^]*)$";

            if (IsKeyExist("MatchParameters"))
            {
                string[] matchParameters = ToStringArrays(configs["MatchParameters"]);
                if (matchParameters != null)
                {
                    foreach (string matchParameter in matchParameters)
                    {
                        if (!Regex.IsMatch(matchParameter, matchRegex))
                            throw new Exception(string.Format(OtherErrors.MatchParametersError1, matchParameter));
                    }
                }
                matchConfig.matchParameters = ToStringArrays(configs["MatchParameters"]);
            }
            else
                throw new Exception(string.Format(OtherErrors.MatchParametersError2));

            //匹配失败是否提示继续
            if (IsKeyExist("NotMatchIsContinue"))
            {
                if (string.IsNullOrEmpty(configs["NotMatchIsContinue"]))
                    matchConfig.notMatchIsContinue = true;//默认值
                else
                {
                    if (!Regex.IsMatch(configs["NotMatchIsContinue"], @"^[01]$"))
                        throw new Exception(OtherErrors.NotMatchIsContinueError);
                    matchConfig.notMatchIsContinue = configs["NotMatchIsContinue"].Trim() == "1" ? true : false;
                }
            }
            else
                matchConfig.notMatchIsContinue = true;

            uniqueInstance = matchConfig;
        }
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        public static MatchConfig GetConfig()
        {
            if (uniqueInstance == null)
            {
                return null;
            }
            return uniqueInstance;
        }
        #endregion
    }  
    #endregion
}
